using HardcoreGame.Content.Tiles.Machines;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using Terraria;
using Terraria.Graphics.Effects;
using Terraria.Graphics.Shaders;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame
{
    public class HardcoreGame : Mod
    {
        private static HardcoreGame _instance;
        public static HardcoreGame Instance
        {
            get
            {
                _instance ??= (HardcoreGame)ModLoader.GetMod("HardcoreGame");

                return _instance;
            }
            set
            {
                _instance = value;
            }
        }        
        public override void Load()
        {
          
            Instance = this;
            
            Filters.Scene["HeatWave"] = new Filter(new ScreenShaderData("FilterHeatDistortion").UseImage("Images/Misc/noise", 0, null).UseIntensity(4f), (EffectPriority)2);
            Filters.Scene["AcidRain"] = new Filter(new ScreenShaderData("FilterBloodMoon").UseColor(0.5f, 1f, 0.25f).UseOpacity(1.25f), (EffectPriority)2);
            Filters.Scene["HeatStroke"] = new Filter(new ScreenShaderData(ModContent.Request<Effect>("HardcoreGame/Assets/Effects/HeatStroke", (ReLogic.Content.AssetRequestMode)2), "HeatStroke"), (EffectPriority)4);
            Filters.Scene["HeatStroke2"] = new Filter(new ScreenShaderData(ModContent.Request<Effect>("HardcoreGame/Assets/Effects/HeatStroke2", (ReLogic.Content.AssetRequestMode)2), "HeatStroke2"), (EffectPriority)4);
            Filters.Scene["HeatStroke4"] = new Filter(new ScreenShaderData(ModContent.Request<Effect>("HardcoreGame/Assets/Effects/HeatStroke4", (ReLogic.Content.AssetRequestMode)2), "HeatStroke4"), (EffectPriority)4);
            Filters.Scene["HeatStroke6"] = new Filter(new ScreenShaderData(ModContent.Request<Effect>("HardcoreGame/Assets/Effects/HeatStroke6", (ReLogic.Content.AssetRequestMode)2), "HeatStroke6"), (EffectPriority)4);
            Filters.Scene["Hypothermia"] = new Filter(new ScreenShaderData(ModContent.Request<Effect>("HardcoreGame/Assets/Effects/Hypothermia", (ReLogic.Content.AssetRequestMode)2), "Hypothermia"), (EffectPriority)4);
        }
        public override void Unload()
        {
            
            Instance = null;
            Filters.Scene["HeatWave"].Deactivate();
            Filters.Scene["AcidRain"].Deactivate();
            Filters.Scene["HeatStroke"].Deactivate();
            Filters.Scene["HeatStroke2"].Deactivate();
            Filters.Scene["HeatStroke4"].Deactivate();
            Filters.Scene["HeatStroke6"].Deactivate();
            Filters.Scene["Hypothermia"].Deactivate();
        }
    }
    internal class DisplayBaseNet
    {
        public static void HandlePacket(BinaryReader reader, int whoAmI)
        {
            byte messageType = reader.ReadByte();

            if (messageType == 0)
            {
                int x = reader.ReadInt32();
                int y = reader.ReadInt32();
                int slot = reader.ReadInt32();
                int netid = reader.ReadInt32();
                int prefix = reader.ReadInt32();
                int stack = reader.ReadInt32();
                DryingRackTE.TryPlacing(x, y, slot, netid, prefix, stack);
            }
        }

        public static void DisplayBaseTryPlace(int x, int y, int slot, int selectedItemIndex, int owner, int stack)
        {
            if (Main.netMode != NetmodeID.SinglePlayer)
            {
                ModPacket packet = HardcoreGame.Instance.GetPacket(256);
                packet.Write(0);
                packet.Write(x);
                packet.Write(y);
                packet.Write(slot);
                Item item = Main.player[owner].inventory[selectedItemIndex];
                packet.Write(item.netID);
                packet.Write(item.prefix);
                packet.Write(stack);
                packet.Send(-1, -1);
            }
        }
    }
}
